﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ReviewCurveApp.Classes;
using System.Data;

namespace ReviewCurveApp.Contorller
{
    public class UnitsManager1
    {
        DBLoader db = new DBLoader();
        public IList<Task> GetUnitsTasksByTimeSpan(DateTime start, DateTime end)
        {
            //TODO :查询任务

            return null;
        }

        public bool AddUnit(Unit unit)
        {
            MyDBCommand command = new MyDBCommand();
            command.Sql = "INSERT INTO test.Units VALUES(NULL, @UnitName, @Description,@CreateDateTime,@SchedualedDateTime,@SubjectId,@Status,@Sequence)";
            command.Parameters.Add("@UnitName", unit.Name);
            command.Parameters.Add("@Description", unit.Description);
            command.Parameters.Add("@CreateDateTime", unit.CreateDateTime);
            command.Parameters.Add("@SchedualedDateTime", unit.ScheduledDateTime);
            command.Parameters.Add("@SubjectId", unit.SubjectId);
            command.Parameters.Add("@Status", unit.Status);
            command.Parameters.Add("@Sequence", unit.Sequence);
            return db.ExcuteQuery(command);
        }

        internal List<Task> getTasksByDay(DateTime dateTime)
        {
            List<Task> tasks = new List<Task>();

            List<Unit> units = GetAllUnits();
            foreach (Unit unit in units)
            {
                tasks.AddRange(unit.GetTasksByDay(dateTime));
            }


            return tasks;
        }

        public List<Unit> GetAllUnits()
        {
            List<Unit> units = new List<Unit>();
            MyDBCommand command = new MyDBCommand();

            command.Sql = string.Format(
                  "select "
                + "A.Id ,A.UnitName,A.Description,A.CreateDatetime,A.SchedualedDateTime,A.SubjectId,A.status,B.Name as SubjectName "
                + " from test.Units A ,test.subjects B where A.subjectId=B.Id"
                  );

            DataTable dt = db.Query(command);

            if (dt == null || dt.Rows.Count <= 0)
            {
                return units;
            }

            foreach (DataRow dr in dt.Rows)
            {
                Unit unit = new Unit();
                unit.Id = DbHelper.GetIntValueFromRow("Id", dr).Value;
                unit.Name = DbHelper.GetStringFromRow("UnitName", dr);
                unit.Description = DbHelper.GetStringFromRow("Description", dr);
                unit.CreateDateTime = DbHelper.GetDateTimeFromRow("CreateDateTime", dr).Value;
                unit.SubjectId = DbHelper.GetIntValueFromRow("SubjectId", dr).Value;
                unit.Status = (UnitStatus)DbHelper.GetIntValueFromRow("Status", dr);
                unit.SubjectInfo = DbHelper.GetStringFromRow("SubjectName", dr);
                unit.ReAssign(unit.CreateDateTime);
                units.Add(unit);
            }
            return units;

        }



        internal List<Unit> GetUnits(Subject subject)
        {
            List<Unit> units = GetAllUnits();

            var query = from unit in units
                        where unit.SubjectId == subject.Id
                        select unit;

            List<Unit> subjectUnits = new List<Unit>();
            foreach (Unit unit in query)
            {
                subjectUnits.Add(unit);
            }

            return subjectUnits;
        }

        internal Unit GetUnitById(string unitId)
        {
            Unit unit = new Unit();
            DataRow dr = getUnitRowById(unitId);
            unit.Id = DbHelper.GetIntValueFromRow("Id", dr).Value;
            unit.Name = DbHelper.GetStringFromRow("UnitName", dr);
            unit.Description = DbHelper.GetStringFromRow("Description", dr);
            unit.CreateDateTime = DbHelper.GetDateTimeFromRow("CreateDateTime", dr).Value;
            unit.SubjectId = DbHelper.GetIntValueFromRow("SubjectId", dr).Value;
            unit.Status = (UnitStatus)DbHelper.GetIntValueFromRow("Status", dr);
            unit.SubjectInfo = DbHelper.GetStringFromRow("SubjectName", dr);
            unit.ReAssign(unit.CreateDateTime);
            return unit;
        }

        private DataRow getUnitRowById(string unitId)
        {
            MyDBCommand command = new MyDBCommand();

            command.Sql = string.Format(
                  "select "
                + "A.Id ,A.UnitName,A.Description,A.CreateDatetime,A.SchedualedDateTime,A.SubjectId,A.status,B.Name as SubjectName "
                + " from test.Units A ,test.subjects B where A.subjectId=B.Id and A.Id=@Id"
                  );
            command.Parameters.Add("@Id", unitId);
            DataTable dt = db.Query(command);

            if (dt == null || dt.Rows.Count <= 0) return null;

            return dt.Rows[0];

        }

    }
}
